<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\CarBookDao;
use app\model\car\CarBook;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use think\exception\ValidateException;

class CarBookServices extends BaseServices
{
    /**
     * CarBookServices constructor.
     * @param CarBookDao $dao
     */
    public function __construct(CarBookDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [], bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        $type = [
            1 => '婚庆用车',
            2 => '短途接送',
            3 => '企业租车',
        ];
        foreach ($list['list'] as &$item){
            $item['type_name'] = $type[$item['type_id']];
            $item['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
        }
        return $list;
    }

    /**
     * 删除
     * @param int $id
     * @return string
     */
    public function delete(int $id){
        $res = $this->dao->update($id,['is_del'=>1,'delete_time'=>time()]);
        if(!$res) throw new AdminException('删除失败,请稍候再试!');
        return '删除成功!';
    }

    /**
     * 处理预约
     * @param int $book_id
     * @param array $op_user
     * @param int $op_user_type
     * @return string
     */
    public function handle(int $book_id,array $op_user,int $op_user_type){
        $res = CarBook::where('id',$book_id)->update(['status'=>1]);
        if(!$res) throw new AdminException('处理失败');
        $title = $op_user['user_name'].'处理了预约申请【'.$book_id.'】';
        BaseLogServices::addLog($book_id,1,$title,$op_user_type,$op_user['id'],$op_user['user_name'],24);
        return '处理成功';
    }

    /**
     * 小程序预约租车
     * @param array $data
     * @return string
     */
    public function carBookSubmit(array $data){
        $data['book_time'] = strtotime($data['book_time']);
        $data['add_time'] = time();
        if(!$this->dao->save($data)) throw new ValidateException('提交失败');
        return '提交成功';
    }
}
